package src.text;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseAdapter;
import java.awt.event.WindowAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.BasicStroke;
import java.awt.Image;
import java.awt.image.BufferedImage;


import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.JEditorPane;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JPopupMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JProgressBar;
import javax.swing.JToolBar;
import javax.swing.JList;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JDialog;
import javax.swing.JComboBox;
import javax.swing.JCheckBox;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.UIManager;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Timer;
import javax.swing.JOptionPane;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.AttributeSet;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import javax.swing.JInternalFrame;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;


import java.text.DateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Scanner;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import java.nio.file.Path;

import javax.imageio.ImageIO;

import net.java.dev.designgridlayout.DesignGridLayout;



import java.lang.reflect.Field;



import java.util.ArrayList;
import java.util.Vector;



//a#############################################################################################
//a##
//a##          myJFrameTextNew -> a new text
//a##
//a##          myJFrameTextNewWithEdit -> a new text with edit(bold,font,fontsize, color...)
//a##
//a##          myJFrameTextOpenWithSaveAs -> open a text ( save + save as)
//a##
//a#############################################################################################
/**
 * 
 *       Author: Rong Tao
 *     Location: UPC
 *         Time: 2017.04
 *    Modify by: Rong Tao
 */
//a#################################
//a##
//a##  myJFrameTextNew -> a new txt
//a##
//a#################################
/**
 * 
 *       Author: Rong Tao
 *     Location: UPC
 *         Time: 2017.04
 *    Modify by: Rong Tao
 */
public class myJFrameTextNew extends JFrame{

    private File FileSave;

    public myJFrameTextNew(){

        final JTextArea JTextAreaNew;
        final FileDialog FileDialogSave;

        setTitle("New Text");
        setBounds(400, 300, 600, 500);	


        JMenuBar JMenuBarSave;
        JMenu JMenuSave;
        JMenuItem JMenuItemSaveClose,JMenuItemSaveSave;


        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(20, 5, 20, 5));

        JMenuBarSave=new JMenuBar();
        JMenuSave=new JMenu("File");
        JMenuItemSaveSave=new JMenuItem("Save");
        JMenuItemSaveClose=new JMenuItem("Close");
		
        JMenuSave.add(JMenuItemSaveSave);
        JMenuSave.add(JMenuItemSaveClose);
        JMenuBarSave.add(JMenuSave);

        JTextAreaNew = new  JTextArea();
        JTextAreaNew.setLineWrap(true);
        JTextAreaNew.setWrapStyleWord(true);
        JTextAreaNew.setBorder(BorderFactory.createEmptyBorder(8, 8, 8, 8));
        JTextAreaNew.setFont(new Font("Georgia", Font.PLAIN, 16));


        JScrollPane pane = new JScrollPane();
        pane.getViewport().add(JTextAreaNew);

        FileDialogSave = new FileDialog(this, "Save", FileDialog.SAVE);
		
        setJMenuBar(JMenuBarSave);

        panel.add(pane);
        add(panel);

        JMenuItemSaveClose.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dispose();
                  }
            });
        JMenuItemSaveSave.addActionListener(new ActionListener() {
			
            @Override
            public void actionPerformed(ActionEvent e) {
                if(FileSave==null){
                    FileDialogSave.setVisible(true);
                    String dirPath=FileDialogSave.getDirectory();
                    String fileName=FileDialogSave.getFile();
                    System.out.println("Dir: "+dirPath+fileName);


                    if(dirPath==null && fileName==null){
                        return;
                          }
 
                    FileSave=new File(dirPath,fileName);

                    try {
                    BufferedWriter bufw=new BufferedWriter( 
                                new FileWriter(FileSave));
                    bufw.write(JTextAreaNew.getText());
                    bufw.close();
						
                    } catch (Exception e2) {
                    throw new RuntimeException("Save Error!");

                          }
                    }
                dispose();
				
                }
           });

           setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
           setVisible(true);
      }
}



